package com.mls.fivedistrict.controller.backend;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.pojo.NewsNewsTag;
import com.mls.fivedistrict.server.NewsNewsTagService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 新闻标签的关系表 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-06-28
 */
@Slf4j
@RestController("backendNewsNewsTagController")
@RequestMapping("/api/manager")
public class NewsNewsTagController extends BaseController {
    @Autowired
    private NewsNewsTagService newsNewsTagService;


    @GetMapping("/v1/newsNewsTags")
    public ServiceResponse getNewsList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
//        List<HotWord> hotWordList = hotWordService.selectList(pageFilter.getWrapper());
        List<NewsNewsTag> newsNewsTagList = newsNewsTagService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo<NewsNewsTag>(newsNewsTagList, 5);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    @GetMapping("/v1/newsNewsTag/{newsNewsTagsId}")
    public ServiceResponse getList(@PathVariable("newsNewsTagId") long newsNewsTagId) {
//        HotWord hotWord = hotWordService.selectById(hotWordId);
        NewsNewsTag newsNewsTag = newsNewsTagService.selectById(newsNewsTagId);
        return ServiceResponse.createSuccessResponse(newsNewsTag);
    }

    @PutMapping("v1/newsNewsTag/{newsNewsTagsId}")
    public ServiceResponse updateById(@PathVariable("newsNewsTagId") long newsNewsTagId, @RequestBody NewsNewsTag newsNewsTag) {
        newsNewsTag.setId(newsNewsTagId);
        boolean update = newsNewsTagService.updateById(newsNewsTag);
        if (update) {
            return ServiceResponse.createSuccessResponse("编辑成功");
        }
        return ServiceResponse.createSuccessResponse("编辑失败");
    }

    @PostMapping("v1/newsNewsTag")
    public ServiceResponse add(@RequestBody NewsNewsTag newsNewsTag) {
        //1所有的添加操作统一校验
        BeanValidator.check(newsNewsTag);
        boolean insert = newsNewsTagService.insert(newsNewsTag);
        if (insert) {
            return ServiceResponse.createSuccessResponse("添加成功");
        }
        return ServiceResponse.createSuccessResponse("添加失败");
    }

    @DeleteMapping("v1/newsNewsTag/{newsNewsTagId}")
    public ServiceResponse deleteById(@PathVariable("newsNewsTagsId") Long newsNewsTagId) {
        boolean insert = newsNewsTagService.deleteById(newsNewsTagId);
        if (insert) {
            return ServiceResponse.createSuccessResponse("删除成功");
        }
        return ServiceResponse.createErrorResponse("删除失败");
    }


}

